<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\Blog\Feedback;
use Carbon\Carbon;
use Illuminate\Http\Request;

class FeedbackService
{

    use Service;

    /**
     * 意见反馈
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FeedbackIndexService( $request )
    {
        $user     = $this->getCommon()->UserCommonService();
        $select   = [
            'id', 'category_id', 'parent_id', 'user_id', 'status', 'updated_at', 'phone', 'content', 'fail', 'static',
        ];
        $where    = function ( $query ) use ( $request, $user ) {
            $query->where('user_id', $user->id);
            if ( $request->filled('static') ) $query->where('static', $request->input('static'));
            if ( $request->filled('name') ) $query->where('content', $request->input('name'));
            if ( $request->filled('category') ) $query->where('category_id', $request->input('category'));
            $query->where('is_show', 1);
            $query->where('status', 1);
            if ( $request->filled('updated_at') ) {
                $time       = $request->input('updated_at');
                $start_time = $time[ 0 ];
                $end_time   = $time[ 1 ];
                $query->whereBetween('updated_at', [ $start_time, $end_time ]);
            }
        };
        $with     = [
            'category' => function ( $query ) {
                $query->select('id', 'name');
            },
            'parent'   => function ( $query ) {
                $query->select('id', 'name');
            },
            'pic'      => function ( $query ) {
                $query->select('id', 'url', 'file_url', 'filename');
            },
        ];
        $feedback = Feedback::GetModel($where, $with, $select, $request->input('limit'));
        return data($feedback);
    }

    /**
     * 删除意见反馈
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FeedbackDestroyService( $id )
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ( $query ) use ( $id, $user ) {
            $query->where('user_id', $user->id);
            if ( ! is_array($id) ) $query->where('id', $id);
            else $query->whereIn('id', $id);
        };
        Feedback::UpdateModel($where, [
            'is_show'    => 2,
            'deleted_at' => Carbon::now()->toDateTimeString(),
        ]);
        return data(NULL, $this->getErrorCode()::message(204));
    }

}
